package service

import (
	"encoding/json"
	"fmt"
	"rongyy/src/middlewares"
	"rongyy/src/models"
)

type PpdService struct {
}

// 公卫，7院卡管中心用， fetch去调用https:// + ip的接口，要报错，直接报dnsname非法，不晓得扎个解决。所以从这里转一哈，艹

// 免挂号就诊
func (rg *PpdService) Mghjz32004(req *models.Req32004) (*models.Resp32004, error) {
	data, err := (&HisService{}).Mghjz32004(req)
	middlewares.Logger.Println("============", string(data))
	d := &models.Resp32004{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", d)
	if d.ResultCode != "0" {
		return nil, fmt.Errorf(d.ResultMsg)
	}
	return d, nil
}

// 保存医嘱/处方
func (op *PpdService) Bcyz32010(req *models.Req32010) (*models.Resp32010, error) {
	data, err := (&HisService{}).Bcyz32010(req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	d := &models.Resp32010{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", data)
	return d, nil
}

func (op *PpdService) FindTodayRecord(patientId string, ptype string) (*models.PpdModel, bool, error) {

	pm := &models.PpdModel{}
	data, has, err := pm.FindTodayRecord(patientId, ptype)
	if err != nil {
		middlewares.Logger.Println("ppd FindTodayRecord failed ", err)
		return nil, false, err
	}
	return data, has, nil
}

func (op *PpdService) Create(req *models.PpdModel) error {
	err := req.Insert()
	if err != nil {
		middlewares.Logger.Println("ppd Create failed ", err)
		return err
	}
	return nil
}
